﻿/*
 * ====================================================================
 * Copyright (c) 2000-2006 MineHe.  All rights reserved.
 *
 * This software is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at http://www.minehe.net/projects/miucos/terms.aspx.
 * If newer versions of this license are posted there, you may use a
 * newer version instead, at your option.
 *
 * This software consists of voluntary contributions made by many
 * individuals.  For exact contribution history, see the revision
 * history and logs, available at http://www.minehe.net/projects/miucos/ or 
 * http://code.google.com/p/miucos/.
 * 
 * WARNNING!!!!!!
 * YOU CAN NOT USE THIS CODE FILE OR ANY CODES IN MIUCOS FILES FOR COMMERCIAL
 * SYSTEM OR PURPOSE.
 * ====================================================================
 */

using System;
using System.Collections.Generic;
using System.Text;

namespace minehe.miucos.common
{
	/// <summary>
	/// SID实用工具类。
	/// </summary>
	/// <creation type="static"/>
	/// <threadsafety static="true"/>
	/// <depends type="class" cref="System.Guid"/>
	/// <remarks>
	/// SID是GUID/UUID的不带"{"、"}"和"-"的字符串形式，
	/// SID不像GUID/UUID有内部对应的数字形式，SID仅有字符串形式，如果要获取SID的对应数字形式，
	/// 则需要将SID转换为GUID/UUID，然后再对该GUID/UUID获取数字形式。
	/// SID和GUID/UUID一样是全球唯一的，
	/// SID可以从任何GUID/UUID转换而来并且也可以转换为对应的GUID/UUID。
	/// SID由固定的32位字符组成。
	/// SID仅有字符串形式，如果要做其他GUID操作则需要使用<see cref="MineHe.MineKylin.Framework.SIDUtils.ConvertSIDToGuid(string)"/>
	/// 转换为<see cref="System.Guid"/>然后对该结构进行操作。
	/// </remarks>
	/// <example>
	/// 43E1E730951E4547AB0AD66348FC056D
	/// </example>
	public static class SIDUtils
	{
		#region Constants Declare

		/// <summary>
		/// 一个SID字符串的字符长度
		/// </summary>
		public const int SID_LENGTH = 32;
		/// <summary>
		/// 一个GUID字符串的字符长度
		/// </summary>
		public const int GUID_LENGTH = 38;
		/// <summary>
		/// 无效空值的SID
		/// </summary>
		public const string SID_EMPTY = "00000000000000000000000000000000";

		#endregion

		#region SID Utility Methods

		/// <summary>
		/// 获取一个新的SID字符串。
		/// </summary>
		/// <returns>新的SID字符串</returns>
		/// <remarks>
		/// 每次调用本方法将都得到一个唯一的、不重复的SID字符串。
		/// </remarks>
		public static string NewSID()
		{
			return SIDUtils.ConvertGuidToSID(Guid.NewGuid());
		}

		/// <summary>
		/// 测试指定的a_sid参数值是否是一个有效的SID字符串。
		/// </summary>
		/// <param name="a_sid">要测试的SID字符串</param>
		/// <returns>如果是有效的SID则返回true，否则返回false</returns>
		/// <remarks>
		/// 测试将把a_sid参数值转换为一个<see cref="System.Guid"/>实例，
		/// 如果转换失败则返回false，否则返回true。
		/// </remarks>
		public static bool IsSID(string a_sid)
		{
			try
			{
				return (SIDUtils.ConvertSIDToGuid(a_sid) != null);
			}
			catch (Exception)
			{
				return false;
			}
		}

		/// <summary>
		/// 将指定的a_sid参数SID字符串转换为对应的<see cref="System.Guid"/>实例。
		/// </summary>
		/// <param name="a_sid">要转换的SID字符串</param>
		/// <returns>转换完成的<see cref="System.Guid"/>实例</returns>
		/// <exception cref="System.InvalidCastException">转换失败</exception>
		public static Guid ConvertSIDToGuid(string a_sid)
		{
			try
			{
				return new Guid("{" + a_sid.Substring(0, 8) + "-" + a_sid.Substring(8, 4) + "-" + a_sid.Substring(12, 4) + "-" + a_sid.Substring(16, 4) + "-" + a_sid.Substring(20, 12) + "}");
			}
			catch (Exception)
			{
				throw new InvalidCastException();
			}
		}

		/// <summary>
		/// 将指定的a_guid参数<see cref="System.Guid"/>转换为对应的SID字符串。
		/// </summary>
		/// <param name="a_guid">要转换的<see cref="System.Guid"/>实例</param>
		/// <returns>转换完成的SID字符串</returns>
		/// <exception cref="System.InvalidCastException">转换失败</exception>
		public static string ConvertGuidToSID(Guid a_guid)
		{
			try
			{
				return a_guid.ToString().Replace("{", "").Replace("-", "").Replace("}", "");
			}
			catch (Exception)
			{
				throw new InvalidCastException();
			}
		}

		#endregion
	}
}
